<template>
  <div style="margin-top:30px">
    <el-form ref="form" :rules="rules" :model="form" label-width="80px">
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="入驻商" prop="shipmentName"  style="margin-right:15px" label-width="120px">
              <el-select v-model="form.shipmentName"  filterable placeholder="请选择入驻商"  :disabled=' isDisabled || operationType==="edit" ' style="width:260px;" @change="shipChange">
                <el-option
                  v-for="item in shipmentList"
                  :key="item.code"
                  :label="item.userName"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="承运商类型" prop="names"  style="margin-right:15px" label-width="120px">
              <el-select v-model="form.carrierType" value-key="code" placeholder="请选择承运商类型" style="width:260px;" :disabled=' isDisabled || operationType==="edit" ' @change="submitForm">
                <el-option
                  v-for="item in typeList"
                  :key="item.value"
                  :label="item.desc"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <!-- 上边是固定的 下边判断 -->
      <div v-if="this.form.carrierType === 'logistics'">
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="承运商编码" prop="carrierCode"  style="margin-right:15px" label-width="120px">
              <el-input v-model="form.carrierCode" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入承运商编码')"/>
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="承运商名称" prop="name"  style="margin-right:15px" label-width="120px">
              <el-input v-model="form.name" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入承运商名称')"/>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="联系人" prop="contacts" style="margin-right:15px" label-width="120px">
              <el-input v-model="form.contacts" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入联系人')"/>
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="手机号" prop="phone" style="margin-right:15px" label-width="120px">
              <el-input v-model="form.phone" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入手机号')" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
       <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="信用代码" prop="businessLicenseImgNumber" style="margin-right:15px" label-width="120px" >
              <el-input v-model="form.businessLicenseImgNumber" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入信用代码')" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="法人姓名" prop="legalPerson" style="margin-right:15px" label-width="120px">
              <el-input v-model="form.legalPerson" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入法人姓名')"/>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="身份证号" prop="idCard" style="margin-right:15px" label-width="120px">
              <el-input v-model="form.idCard" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入身份证号')" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="税点(%)" prop="taxPoint" style="margin-right:15px" label-width="120px">
              <el-input v-model="form.taxPoint" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入税点(%)')" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
       <el-col :span="12">
          <el-form-item label="所在区域" prop="areaList" style="margin-right:15px"  label-width="120px">
            <el-cascader
              v-model="form.areaList"
              :options="parentData.areaOptions"
              :disabled='isDisabled'
              :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请选择')"
              :props="{
                value: 'countyName',
                label: 'countyName',
                children: 'children',
              }"
              style="width:260px;"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="详细地址" prop="address" style="margin-right:15px" label-width="120px">
              <el-input v-model="form.address" :disabled='isDisabled'  :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入详细地址')"/>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="是否结算" prop="isSettlement" style="margin-right:15px" label-width="120px">
              <el-select v-model="form.isSettlement" :disabled='isDisabled' clearable  style="width:260px;" :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请选择是否结算')">
                <el-option value="0" label="不结算"/>
                <el-option value="1" label="结算"/>
              </el-select>
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="备注" prop="remark" style="margin-right:15px" label-width="120px">
              <el-input v-model="form.remark" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入备注')" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      </div>
      <!-- 分割 -->
      <div v-if="this.form.carrierType === 'express'">
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="承运商编码" prop="carrierCode"  style="margin-right:15px" label-width="120px">
              <el-input v-model="form.carrierCode" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入承运商编码')"/>
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="承运商名称" prop="name"  style="margin-right:15px" label-width="120px">
              <el-input v-model="form.name" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入承运商名称')"/>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <!-- <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="承运商名称" prop="name" style="margin-right:15px" label-width="120px">
              <el-input v-model="form.name" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入承运商名称')" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="外部编码" prop="outerCode"  style="margin-right:15px" label-width="120px">
              <el-input v-model="form.outerCode" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入外部编码')" />
            </el-form-item>
          </div>
        </el-col>
      </el-row> -->
      <el-row>
        <!-- <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="电子面单客户号" prop="waybillCustomerCode"  style="margin-right:15px" label-width="120px">
              <el-input v-model="form.waybillCustomerCode" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入电子面单客户号')"/>
            </el-form-item>
          </div>
        </el-col> -->
        <el-col :span="12">
          <!-- <div class="grid-content bg-purple">
            <el-form-item label="电子面单密码" prop="waybillPassword"  style="margin-right:15px" label-width="120px">
              <el-input v-model="form.waybillPassword" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入电子面单密码')"/>
            </el-form-item>
          </div> -->
          <div class="grid-content bg-purple">
            <el-form-item label="平台类型" prop="platformType" style="margin-right:15px" label-width="120px">
              <el-select v-model="form.platformType" @change="changePlatformType" :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请选择')" :disabled='isDisabled' style="width:100%">
                <el-option
                  v-for="item in platformTypeList"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="关联店铺" prop="outerCode"  style="margin-right:15px" label-width="120px">
              <el-select v-model="form.shopCode" @change="shopChange" filterable :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请选择')" :disabled='isDisabled' clearable style="width:100%">
                <el-option
                  v-for="item in associatedShop"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <!-- <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="外部编码" prop="outerCode"  style="margin-right:15px" label-width="120px">
              <el-input v-model="form.outerCode" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入外部编码')" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="平台类型" prop="platformType" style="margin-right:15px" label-width="120px">
              <el-select v-model="form.platformType" filterable :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请选择')" :disabled='isDisabled' clearable style="width:100%">
                <el-option
                  v-for="item in platformTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </div>
        </el-col>
      </el-row> -->
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="发货人" prop="senderName"  style="margin-right:15px" label-width="120px">
              <el-input v-model="form.senderName" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入发货人')" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="手机号" prop="senderMobile"  style="margin-right:15px" label-width="120px">
              <el-input v-model="form.senderMobile" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入手机号')" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="所在区域" prop="areaList" style="margin-right:15px"  label-width="120px">
            <el-cascader
              v-model="form.areaList"
              :options="parentData.areaOptions"
              :disabled='isDisabled'
              :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请选择')"
              :props="{
                value: 'countyName',
                label: 'countyName',
                children: 'children',
              }"
              style="width:260px;"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="详细地址" prop="address" style="margin-right:15px" label-width="120px">
              <el-input v-model="form.address" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入详细地址')" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="模板类型" prop="standardTemplateName"  style="margin-right:15px" label-width="120px">
               <el-select v-model="form.standardTemplateName" :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请选择模板类型')"  :disabled='isDisabled' @visible-change="getType" @change="changTypeVal" style="width:100%;" >
                <el-option
                  v-for="item in templateTypeList"
                  :key="item.templateName"
                  :label="item.templateName"
                  :value="item"
                />
            </el-select>
              <!-- <el-input v-model="form.templateType" :  /> -->
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="模板名称" prop="templateName"  style="margin-right:15px" label-width="120px">
              <el-input v-model="form.templateName" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入模板名称')" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="快递模板地址" prop="templateAddress"  style="margin-right:15px" label-width="120px">
              <el-input v-model="form.templateAddress" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入快递模板地址')" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="模板编码" prop="templateCode"  style="margin-right:15px" label-width="120px">
              <el-input v-model="form.templateCode" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入模板编码')" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="外部编码" prop="outerCode"  style="margin-right:15px" label-width="120px">
              <el-input v-model="form.outerCode" @change="changeOuterCode" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入外部编码')" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12" v-if="form.outerCode ==='SF'">
          <div class="grid-content bg-purple">
            <el-form-item label="快递品牌编码" prop="brandCode"  style="margin-right:15px" label-width="120px">
              <el-input v-model="form.brandCode" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '快递品牌编码,仅适用于淘宝顺丰')" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row v-if="form.outerCode ==='SF'">
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="月结卡号" style="margin-right:15px" label-width="120px">
              <el-input v-model="form.customerCode" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '淘宝顺丰月结卡号')" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="产品类别" style="margin-right:15px" label-width="120px">
              <el-select style="width:100%" v-model="form.expressType" placeholder="请选择快件产品类别" clearable>
                <el-option value="1" label="顺丰标快"/>
                <el-option value="2" label="顺丰特惠"/>
              </el-select>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row v-if="form.outerCode ==='SF'">
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="付款方式" style="margin-right:15px" label-width="120px">
              <el-select style="width:100%" v-model="form.expressPayMethod" placeholder="请选择快递费付款方式" clearable>
                <el-option value="1" label="寄方付"/>
                <el-option value="2" label="收方付"/>
                <el-option value="3" label="第三方付"/>
              </el-select>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <template v-if="form.platformType ==='JD'">
        <el-row>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="承运商ID" prop="providerId"  style="margin-right:15px" label-width="120px">
                <el-input v-model="form.providerId" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入承运商ID')" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="商家编码" prop="vendorCode"  style="margin-right:15px" label-width="120px">
                <el-input v-model="form.vendorCode" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入商家编码')" />
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="商家名称" prop="vendorName"  style="margin-right:15px" label-width="120px">
                <el-input v-model="form.vendorName" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入商家名称')" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="发货网点编码" prop="branchCode"  style="margin-right:15px" label-width="120px">
                <el-input v-model="form.branchCode" :disabled='isDisabled' :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请输入承运商发货网点编码')" />
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </template>
      <!-- <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="选择打印机" style="margin-right:15px" label-width="120px">
              <el-select v-model="form.printerName" filterable :placeholder="operationType == 'detail' ? ' ' :(operationType == 'audit' ? ' ' : '请选择')" @change="changeShopCode" :disabled='isDisabled' clearable style="width:100%">
                <el-option
                  v-for="item in printerList"
                  :key="item"
                  :label="item"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </div>
        </el-col>
      </el-row> -->
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer w240" v-if="this.operationType === 'audit'">
      <el-button type="primary" @click="audit('1')">审核通过</el-button>
      <el-button type="danger" @click="audit('2')">审核不通过</el-button>
    </div>
    <div slot="footer" class="dialog-footer" v-if="!isDisabled">
      <el-button type="primary" @click="submitData" :disabled='isDisabled' plain>确 定</el-button>
    </div>
  </div>
</template>
<script>
  import Qs from 'qs'
  import request from '@/utils/request'
  import { carrierAdd, carrierEdit, carrierDetail, carrierAudit , getCarrierType, shopList } from '@/api/basicInfo'
  import { SelectAddress } from '@/api/orderform';
  import { getListDict } from '@/api/commonApi';
  import {
    validatePhone,
    validateBusinessLicenseImgNum,
    validateIDNumber,
    isEmpty
  } from "@/utils/validate.js";
  import getPrinterList from '@/components/Printing/getPrinters.js'
  export default {
    name: 'carrierOperation',
    props: {
      operationType: {
        type: String,
        default: 'add'
      },
      operationCode: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
         parentData: {
          areaOptions: []
        },   //选择区域列表
        isDisabled: ['detail', 'audit'].includes(this.operationType),
        templateTypeList:[],
        form: {
          qimenKey:"",
          waybillPassword:"",
          waybillCustomerCode:"",
          outerCode:'',
          carrierCode:'',
          name:'',
          contacts:'',
          phone:'',
          businessLicenseImgNumber:'',
          legalPerson:'',
          idCard:'',
          taxPoint:'',
          merchantsName:'',
          address:'',
          isSettlement:'',
          remark:'',
          areaList:'',
          status: '0',
          province:"",
          city:"",
          county:"",
          warehouseName:'',
          shipmentName:'',
          shopName:'',
          shopCode:'',
          carrierType: 'logistics',
          standardTemplateName:'',
          standardTemplateUrl:'',
          standardWaybillType:'',
          platformType: '', // 平台类型
        },
        rules: {
          shipmentName: [
            {required: true, message: '请选择入驻商', trigger: 'blur'},
          ],
          carrierCode: [
            {required: true, message: '请输入承运商编码', trigger: 'blur'},
          ],
          contacts: [
            {required: true, message: '请输入联系人', trigger: 'blur'},
          ],
          name: [
            {required: true, message: '请输入承运商名称', trigger: 'blur'},
          ],
          brandCode: [
            {required: true, message: '请输入快递品牌编码', trigger: 'blur'},
          ],
          isSettlement:[
            {required: true,  message: "请选择是否结算", trigger: "blur" },
          ],
          platformType: [
            {required: true, message: '请选择平台类型', trigger: 'blur'},
          ],
          // 手机号
          senderMobile: [
              {
              required: false,
              validator(rule, value, callback) {
                 if (!value) {
                  return callback()
                }
                validatePhone(value)
                  ? callback()
                  : callback(new Error("手机号格式有误"));
              },
              trigger: "blur",
            },
          ],
            // 手机号
          phone: [
              {
              required: true,
              validator(rule, value, callback) {
                validatePhone(value)
                  ? callback()
                  : callback(new Error("手机号格式有误"));
              },
              trigger: "blur",
            },
          ],
          //
          idCard:[
            {
              validator(rule, value, callback){
                // 当数据为空时，不进行校验
                if (!value) {
                  return callback()
                }
                validateIDNumber(value)
                ?callback()
                :callback(new Error("输入格式不正确"))
              },
               trigger: "blur"
            }
          ],
          remark:[
            {required: false, max:64, message: "长度不能超过64个字符", trigger: "change"},
          ],
          address:[
             {required: false, max: 64, message: "长度不能超过64个字符", trigger: "change" },
          ],
           // 信用代码
          businessLicenseImgNumber:[
             {
              validator(rule, value, callback){
                // 当数据为空时，不进行校验
                if (!value) {
                  return callback()
                }
                validateBusinessLicenseImgNum(value)
                ?callback()
                :callback(new Error("请检查格式是否正确"))
              },
              trigger: "blur"
            }
          ],
          // 姓名
          legalPerson:[
              { required:false, pattern: /^[a-zA-Z\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/, message: '姓名输入格式有误！', trigger: 'change'},
              {max:24, message: '字符长度不能超过24位', trigger: 'change'},
              {min:1, message: '姓名不能少于1位', trigger: 'change'}
          ],
           // 税点
          taxPoint: [
            // validatenumber
            { required:false, pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: '输入格式有误！请输入正数', trigger: 'change'}
          ],
        },
        shipmentList:[],
        typeList:[],
        warehouses: [],
        shopCodeList: [],
        platformTypeList: [], // 平台类型
        associatedShop: [], // 关联店铺
        printerList: [], // 打印机列表
      }
    },
    created(){
      this.findShipmentList()
      SelectAddress().then(response => {
        this.parentData.areaOptions = response.data.records
      })
    },
    mounted() {
      if(this.operationType !='add'){
        this.getInfo()
        this.getShipmentList()
      }
    },
    methods: {
      // 获取打印机列表
      getPrinters() {
        if (this.printerList.length === 0) {
          // new getPrinterList(res => {
          //   if (res.status ===0) {
          //     this.printerList = res.list
          //   }
          // }).initSocket()
        }
      },
      changePlatformType(sourcePlatformCode) {
        this.form.shopCode = ''
        this.form.standardTemplateName = ''
        shopList({
          shipmentCode: this.form.shipmentCode,
          sourcePlatformCode
        }).then(res => this.associatedShop = res.data)
      },
      // 入驻商 承运商类型
      findShipmentList() {
        request({
          url: '/wms/warehouseShipment/business/shipments',
          method: 'get',
        }).then(response => {
          this.shipmentList = response.data
          if(this.shipmentList.length === 1){
            this.form.shipmentCode = this.shipmentList[0].partyCode
            this.form.shipmentName = this.shipmentList[0].userName
          }
        })
        request({
          url: '/wms/Carrier/carrierTypes',
          method: 'get',
        }).then(response => {
          this.typeList = response.data
        })
        request({
          url: '/wms/warehouse/business/warehouse',
          method: 'get',
        }).then(response => {
          this.warehouses = response.data
        })
        request({
          url: '/wms/shop/list?status=1',
          method: 'post',
        }).then(response => {
          this.shopCodeList = response.data.records
        })
      },
      getType(e){
        let { waybillCustomerCode, waybillPassword, outerCode, qimenKey, platformType } = this.form
        if(e===true){
          if(isEmpty(waybillCustomerCode) || isEmpty(outerCode) || isEmpty(platformType)){
            this.$message.error('请检查店铺授权token、外部编码、平台类型是否已填写')
          } else {
            let parms={ qimenKey, accessToken: waybillCustomerCode,waybillPassword,outerCode,platformType }
            getCarrierType(parms).then(res=>{
              if(res.status === 0){
                // this.templateTypeList = res.data[0].standardTemplateDOs;
                this.templateTypeList = res.data || []
              } else {
                this.$message.error(res.msg);
              }
            })
          }
        }

      },
      changTypeVal(e){
        this.form.standardTemplateName = e.templateName
        this.form.standardTemplateUrl = e.templateUrl
        this.form.standardWaybillType = typeof e.standardWaybillType === 'object' ? '' : e.standardWaybillType
      },
      // 下拉
      shipChange(e){
        this.form.shipmentCode = e.partyCode
        this.form.shipmentName = e.userName
        this.getShipmentList()
      },
      // 获取枚举数据，平台类型列表
      getShipmentList() {
        this.form.platformType = ''
        this.form.shopCode = ''
        this.form.standardTemplateName = ''
        getListDict({ types: 'carrierPlatform' }).then(response => {
          this.platformTypeList = response.data.filter(item => item.type === 'carrierPlatform')[0]['dictList']
        })
      },
      changeWarehouse(e){
        this.form.warehouseCode = e.code
        this.form.warehouseName = e.name
      },
      changeShopCode(e){
        this.form.shopName = e.name
        this.form.shopCode = e.code
      },
      // 获取页面信息
      getInfo() {
         carrierDetail(this.operationCode).then(res=>{
           if(res.status === 0){
              shopList({
                shipmentCode: res.data.shipmentCode,
                sourcePlatformCode: res.data.platformType
              }).then(shop => {
                this.associatedShop = shop.data
                const { accessToken } = shop.data.find((item) => item.code === res.data.shopCode)
                this.form.waybillCustomerCode = accessToken
              })
             this.form = res.data
             this.form.areaList = [ this.form.province, this.form.city, this.form.county ]
             this.form.waybillCustomerCode = res.data.accessToken

             if (res.data.carrierType === 'express') this.getPrinters()
           }
         })
      },
      // 提交数据 - 新增 or 编辑
      submitData() {
        let [ province, city, county ] = this.form.areaList
        this.form.province = province;
        this.form.city = city;
        this.form.county = county;
        this.$refs['form'].validate((valid) => {
          if (valid) {
            let parms = JSON.parse(JSON.stringify(this.form))
            let requestOperation = carrierAdd
            if (this.operationType === 'add') {
              parms.createdBy = this.$store.getters.authorizeDataCode
              parms.createdName = this.$store.getters.name
            } else {
              parms.modifiedBy = this.$store.getters.authorizeDataCode
              parms.modifiedName = this.$store.getters.name
              requestOperation = carrierEdit
            }
            requestOperation(parms).then(res=>{
              this.$message.success(res.msg);
              if(res.status === 0){
                this.$emit('operationComplete', this.operationType)
              }
            })
            // const requestOperation = this.operationType === 'add' ? carrierAdd : carrierEdit
            // requestOperation(this.form).then(res=>{
            //   this.$message.success(res.msg);
            //   if(res.status === 0){
            //     this.$emit('operationComplete', this.operationType)
            //   }
            // })
          } else {
            return this.$message.error('请检查所填内容');
          }
        });
      },
      // 审核 - 通过 or 拒绝
      audit(status) {
        const params = {
          status,
          code: this.operationCode,
          approvedBy: this.$store.getters.code,
          approvedName: this.$store.getters.name
        }
        carrierAudit(Qs.stringify(params)).then(res=>{
          this.$message.success(res.msg);
          if(res.status === 0){
            this.$emit('operationComplete', this.operationType)
          }
        })
      },
      shopChange(code) {
        this.form.standardTemplateName = ''
        this.form.waybillCustomerCode = ''
        if (code) {
          const { name: shopName, accessToken } =  this.associatedShop.find((item) => item.code === code)
          this.form.shopName = shopName
          this.form.waybillCustomerCode = accessToken
        }
      },
      changeOuterCode() {
        this.form.standardTemplateName = ''
      },
      // 重置
      submitForm(){
        let shipmentName = this.form.shipmentName
        let shipmentCode = this.form.shipmentCode
        this.$refs['form'].resetFields()
        this.form.shipmentName = shipmentName
        this.form.shipmentCode = shipmentCode

        this.getPrinters()
      }
    }
  }
</script>
<style lang="scss" scoped>
  .dialog-footer {
    margin: 15px 0 15px 0;
    width: 90px;
    float: right;
  }
  .w240 {
    width: 240px;
  }
  .el-select-dropdown__item.selected{
    color: #606266;
    font-weight: 400;
  }
</style>
